package com.chen.pspms.controller;

import com.chen.pspms.entity.CompMajorStatistics;
import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.GoveMajorStatistics;
import com.chen.pspms.entity.Majorhazardregistertable;
import com.chen.pspms.service.CompMajorStatisticsService;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.service.MajorhazardregisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/majorhazardregister")
public class MajorhazardregisterController {
    @Autowired
    MajorhazardregisterService majorhazardregisterService;
    @Autowired
    CompanyService companyService;
    @Autowired
    CompMajorStatisticsService compMajorStatisticsService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<CompanyInfo> companylist = companyService.findAll();
        //根据企业信息，查询企业危险源统计信息
        List<CompMajorStatistics> compMajorStatisticsList = compMajorStatisticsService.getListByCompany(companylist);
        //存储信息
        model.addAttribute("compMajorStatisticsList",compMajorStatisticsList);
        //处理业务返回结果
        return "重大危险源管理/管理重大危险源管理页面";
    }

    @RequestMapping("/glance")
    public String glance(Integer id, Model model){
        //根据公司id，返回危险源信息，不返回无的
        //查询公司
        CompanyInfo company = companyService.findyById(id);
        //查询企业危险源信息列表
        List<Majorhazardregistertable> allMajorhazardregisterList=majorhazardregisterService.findyListByComId(id);
        //去除正常的
        List<Majorhazardregistertable> majorhazardregisterList=majorhazardregisterService.removeSafe(allMajorhazardregisterList);
        model.addAttribute("company",company);
        model.addAttribute("majorhazardregisterList",majorhazardregisterList);
        return "重大危险源管理/管理浏览";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        majorhazardregisterService.delete(id);
        return "redirect:/majorhazardregister/list";
    }

    @RequestMapping("/dataonline")
    public String dataonline(Model model){
        //<!--地区：揭阳 一级：** 二级：** 三级：** 预警：**  巡检：**-->
        List<CompanyInfo> companyList=companyService.findAll();
        List<CompMajorStatistics> compMajorStatisticsList = compMajorStatisticsService.getListByCompany(companyList);
        //弄一个城市set
        Map<String, GoveMajorStatistics> cityMap=new HashMap<>();
        GoveMajorStatistics goveMajorStatistics;
        for(CompMajorStatistics compMajorStatistics:compMajorStatisticsList){
            String city=compMajorStatistics.getCompany().getCity();
            if(!cityMap.containsKey(city)){
                goveMajorStatistics= new GoveMajorStatistics(city,0,0,0,0,0);
            }else{
                goveMajorStatistics= cityMap.get(city);
            }
            goveMajorStatistics.setInspectionNum(goveMajorStatistics.getInspectionNum()+compMajorStatistics.getInspectionNum());
            goveMajorStatistics.setLevel1Num(goveMajorStatistics.getLevel1Num()+compMajorStatistics.getLevel1Num());
            goveMajorStatistics.setLevel2Num(goveMajorStatistics.getLevel2Num()+compMajorStatistics.getLevel2Num());
            goveMajorStatistics.setLevel3Num(goveMajorStatistics.getLevel3Num()+compMajorStatistics.getLevel3Num());
            goveMajorStatistics.setWarningNum(goveMajorStatistics.getWarningNum()+compMajorStatistics.getWarningNum());
            goveMajorStatistics.setMajorhazardNumByDangerous();
            cityMap.put(city,goveMajorStatistics);
        }
        List<GoveMajorStatistics> goveMajorStatisticsList=new ArrayList<>();
        for(GoveMajorStatistics goveMajorStatistics1:cityMap.values()){
            goveMajorStatisticsList.add(goveMajorStatistics1);
        }
        model.addAttribute("goveMajorStatisticsList",goveMajorStatisticsList);
        return "重大危险源管理/数据在线监控";
    }

//    @RequestMapping("/testonline")
//    public String testonline(Model model){
//        return "重大危险源管理/测试实时";
//    }
}
